package com.towexin.application.system.controller;

import com.towexin.common.ResultData;
import com.towexin.config.mylogging.LogType;
import com.towexin.config.mylogging.MyLog;
import com.towexin.application.system.domain.Menu;
import com.towexin.application.system.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author 唐伟鑫
 * @version 1.0
 * @date 2021/4/11 21:06
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService service;

    @MyLog(type = LogType.INSERT, operation = "增加新菜单")
    @PostMapping("save")
    @RequiresPermissions("system.menu.add")
    public ResultData saveMenu(Menu menu){
        return service.saveOrUpdateMenu(menu)?ResultData.OK():ResultData.NO();
    }

    @MyLog(type = LogType.UPDATE, operation = "修改菜单信息")
    @PostMapping("update")
    @RequiresPermissions("system.menu.edit")
    public ResultData updateMenu(Menu menu){
        return service.saveOrUpdateMenu(menu)?ResultData.OK():ResultData.NO();
    }

    @MyLog(type = LogType.DELETE, operation = "删除菜单")
    @GetMapping("deleteOne")
    @RequiresPermissions("system.menu.delete")
    public ResultData deleteOne(String id){
        return service.deleteCascade(id)?ResultData.OK():ResultData.NO();
    }

    @MyLog(type = LogType.DELETE, operation = "删除菜单")
    @GetMapping("deleteMore")
    @RequiresPermissions("system.menu.delete")
    public ResultData deleteMore(List<String> ids){
        return service.deleteCascadeMore(ids)?ResultData.OK():ResultData.NO();
    }

    /**
     * 菜单渲染
     * @return
     */
    @GetMapping("menuRendering")
    public ResultData menuRendering(){
        return ResultData.OK(service.menuRendering());
    }

    @GetMapping("newMenuRendering")
    public List<Map<String, Object>> newMenuRendering(){
        return service.newMenuRendering();
    }

    @GetMapping("selectMenuTree")
    public ResultData selectMenuTree(){
        return ResultData.OK(service.selectMenuTree());
    }

    @GetMapping("selectMenuTreeByPid")
    public ResultData selectMenuTreeByPid(String pId, String id){
        return ResultData.OK(service.selectMenuTreeByPid(pId, id));
    }

    @MyLog(type = LogType.UPDATE, operation = "修改状态")
    @RequiresPermissions("system.menu.status")
    @GetMapping("changeStatus")
    public ResultData changeMenuStatus(String id, Boolean status){
        return service.changeStatus(id, status)?ResultData.OK():ResultData.NO();
    }

    //@MyLog(type = 1, operation = "获取菜单列表")
    @RequiresPermissions("system.menu.list")
    @GetMapping("selectAll")
    public ResultData selectAll(){
        return ResultData.OK(service.selectAll());
    }

    @GetMapping("openStatus")
    public ResultData openStatus(String id, Boolean open){
        Menu menu = new Menu();
        menu.setId(id);
        menu.setOpen(open);
        return service.updateById(menu)?ResultData.OK():ResultData.NO();
    }
}
